/**
 * @file tiny_driver.c
 * @author 张恒宣
 * @LastEditTime 2025/5/08
 *
 * MIT License
 *
 * Copyright (c) 2025 张恒宣 luckkeymail@163.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "tiny_driver.h"
#include "tiny_malloc.h"
#include <string.h>
#include "tiny_hash.h"

static tiny_list_t tiny_driver_list = {0};


/// @brief 注册设备
/// @param name 设备名称
/// @param op  设备操作函数
/// @param id  id
/// @return -2 内存不足 -1 参数错误 -2 设备已存在 1 成功
int tiny_dev_regsiter(const char *name, const tiny_operator_t *op, tiny_uint32_t arg)
{
    if (!name || !op)
    {
        return -1;
    }

    if (tiny_dev_get(name))
    {
        return -2;
    }

    tiny_driver_t *driver = (tiny_driver_t *)tiny_malloc(sizeof(tiny_driver_t));

    if (!driver)
    {
        return -2;
    }

    driver->name = name;
    driver->hash = tiny_hash(name);
    driver->op = op;
		driver->arg = arg;

    tiny_init_list_node(&driver->driver_link);
    tiny_list_insert_pre(&tiny_driver_list, &driver->driver_link);
    return 1;
}

/// @brief 注销设备
/// @return -1 参数错误 -2 设备不存在 1 成功
int tiny_dev_unregsiter(const char *name)
{
    if (!name)
    {
        return -1;
    }

    tiny_driver_t *driver = (tiny_driver_t *)tiny_dev_get(name);
    if (!driver)
    {
        return -2;
    }

    tiny_remove_node_from_list(&driver->driver_link);
    tiny_free(driver);

    return 1;
}

/// @brief 获取设备
/// @param name 设备名称
/// @return tiny_null失败
const tiny_driver_t *tiny_dev_get(const char *name)
{
    if (!name)
    {
        return tiny_null;
    }
    tiny_uint32_t hash = tiny_hash(name);
    tiny_list_node_t *node = tiny_driver_list.head;
    for (int i = 0; i < tiny_driver_list.len; ++i)
    {
        tiny_driver_t *driver = TINY_DRIVER_ENTRY(node);
        if (driver->hash == hash && !strcmp(driver->name, name))
        {
            return driver;
        }
        node = node->next;
    }
    return tiny_null;
}

/// @brief 扫描设备列表
/// @param callback 回调函数
void ting_devs_scan(int (*callback)(const tiny_driver_t *driver))
{
    if (!callback)
    {
        return;
    }

    tiny_list_node_t *node = tiny_driver_list.head;
    for (int i = 0; i < tiny_driver_list.len; ++i)
    {
        tiny_driver_t *driver = TINY_DRIVER_ENTRY(node);
        if (!callback(driver))
        {
            break;
        }
        node = node->next;
    }
}
